﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Match.Foundation.Enums;
using Match.ManagementApplication.AbstractClasses;
using Match.ManagementApplication.Classes;
using System.IO;
using Match.Foundation.Business.Classes;

namespace Match.ManagementApplication.Forms
{
    public partial class frmDataImport : MatchForm
    {
        private List<Race> Races = null;
        private List<StartClass> StartClasses = null;
        private List<Participant> Participants = null;

        public frmDataImport(FormName FormName, String ShownName, String ShownDescription)
            : base(FormName, ShownName, ShownDescription)
        {
            InitializeComponent();
            this.Name = ShownName;
            Races = new List<Race>();
            StartClasses = new List<StartClass>();
            Participants = new  List<Participant>();
        }

        private void buttonOpenFile1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Importdatei auswählen!";
            openFileDialog1.ShowDialog();
            textBoxImportFilePath.Text = openFileDialog1.FileName;
            SetAvailableHeadlines();
        }
        private void SetAvailableHeadlines()
        {
            List<String> Entrys = GetFileEntrys();

            FillCombobox(comboBoxCity, GetLineEntrys(Entrys[0]));
            FillCombobox(comboBoxClub, GetLineEntrys(Entrys[0]));
            FillCombobox(comboBoxDistance, GetLineEntrys(Entrys[0]));
            FillCombobox(comboBoxEmail, GetLineEntrys(Entrys[0]));
            FillCombobox(comboBoxFirstName, GetLineEntrys(Entrys[0]));
            FillCombobox(comboBoxGebDate, GetLineEntrys(Entrys[0]));
            FillCombobox(comboBoxGender, GetLineEntrys(Entrys[0]));
            FillCombobox(comboBoxLastName, GetLineEntrys(Entrys[0]));
            FillCombobox(comboBoxPhone, GetLineEntrys(Entrys[0]));
            FillCombobox(comboBoxPLZ, GetLineEntrys(Entrys[0]));
            FillCombobox(comboBoxStartclass, GetLineEntrys(Entrys[0]));
            FillCombobox(comboBoxStreet, GetLineEntrys(Entrys[0]));
            FillCombobox(comboBoxRegKey, GetLineEntrys(Entrys[0]));
        }
        private List<String> GetFileEntrys()
        {
            FileStream Stream = null;
            StreamReader Reader = null;

            List<String> Entrys = new List<string>();

            try
            {
                Stream = new FileStream(textBoxImportFilePath.Text, FileMode.Open);
                Reader = new StreamReader(Stream);

                while(!Reader.EndOfStream)
                {
                    Entrys.Add(Reader.ReadLine());
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (Stream != null) Stream.Close();
                if (Reader != null) Reader.Close();
            }
            return Entrys;
        }
        private List<String> GetLineEntrys(String Line)
        {
            List<String> Entrys = new List<string>();
            foreach (string s in Line.Split(';'))
            {
                String Strg = s;
                if (Strg.StartsWith("\"") && Strg.EndsWith("\""))
                {
                    Strg = Strg.Substring(1, Strg.Length - 1);
                    Strg = Strg.Substring(0, Strg.Length - 1);
                }
                Entrys.Add(Strg);
            }
            return Entrys;
        }
        private void FillCombobox(ComboBox Box, List<String> List)
        {
            for(int i =0; i<List.Count; i++)
            {
                Box.Items.Add(List[i]);
            }
            Box.Refresh();
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            List<String> Entrys = GetFileEntrys();

            for (int i = 1; i < Entrys.Count; i++)
            {

                SetProgressbar(Convert.ToInt32(100/Convert.ToDouble(Entrys.Count) * Convert.ToDouble(i)));

                List<String> LineValues = GetLineEntrys(Entrys[i]);

                if (!CheckRace(LineValues[comboBoxDistance.SelectedIndex]))
                {
                    Race Race = new Race();
                    Race.Name = LineValues[comboBoxDistance.SelectedIndex];
                    Races.Add(Race);
                }
                if (!CheckStartClass(LineValues[comboBoxStartclass.SelectedIndex]))
                {
                    StartClass StartClass = new StartClass();
                    StartClass.Name = LineValues[comboBoxStartclass.SelectedIndex];
                    StartClasses.Add(StartClass);
                }

                Participant Participant = new Participant();
                Participant.FirstName = LineValues[comboBoxFirstName.SelectedIndex];
                Participant.LastName = LineValues[comboBoxLastName.SelectedIndex];
                Participant.Street = LineValues[comboBoxStreet.SelectedIndex];
                Participant.City = LineValues[comboBoxCity.SelectedIndex];
                Participant.Zip = LineValues[comboBoxPLZ.SelectedIndex];
                Participant.Birthday = Convert.ToDateTime(LineValues[comboBoxGebDate.SelectedIndex]);
                Participant.Telefon = LineValues[comboBoxPhone.SelectedIndex];
                Participant.Email = LineValues[comboBoxEmail.SelectedIndex];
                Participant.Club = LineValues[comboBoxClub.SelectedIndex];
                if (LineValues[comboBoxGender.SelectedIndex].Equals("Mann"))
                    Participant.Gender = GenderType.Mann;
                else Participant.Gender = GenderType.Frau;

                Participant.RegistrationKey = LineValues[comboBoxRegKey.SelectedIndex];

                Participant.StartClass = GetStartClass(LineValues[comboBoxStartclass.SelectedIndex]);
                Participant.Race = GetRace(LineValues[comboBoxDistance.SelectedIndex]);

                Participants.Add(Participant);
            }

            for(Int32 i = 0;i<Races.Count; i++)
            {
                SetProgressbar(Convert.ToInt32(100 / Convert.ToDouble(Races.Count) * Convert.ToDouble(i)));
                Races[i].Save();
            }

            for (int i = 0; i < StartClasses.Count; i++ )
            {
                SetProgressbar(Convert.ToInt32(100 / Convert.ToDouble(StartClasses.Count) * Convert.ToDouble(i)));
                StartClasses[i].Save();
            }

            for (int i = 0; i < Participants.Count; i++)
            {
                SetProgressbar(Convert.ToInt32(100 / Convert.ToDouble(Participants.Count) * Convert.ToDouble(i)));
                Participants[i].Save();
            }

            MessageBox.Show("Import Beendet!", "Ende", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
        public Boolean CheckRace(String RaceName)
        {
            foreach (Race Race in Races)
            {
                if (Race.Name.Equals(RaceName))
                    return true;
            }
            return false;
        }
        public Boolean CheckStartClass(String StartClassName)
        {
            foreach (StartClass StartClass in StartClasses)
            {
                if (StartClass.Name.Equals(StartClassName))
                    return true;
            }
            return false;
        }
        public Race GetRace(String RaceName)
        {
            foreach(Race Race in Races)
            {
                if (Race.Name.Equals(RaceName))
                    return Race;
            }
            return null;
        }
        public StartClass GetStartClass(String StartClassName)
        {
            foreach (StartClass StartClass in StartClasses)
            {
                if (StartClass.Name.Equals(StartClassName))
                    return StartClass;
            }
            return null;
        }
        public void SetProgressbar(Int32 Value)
        {
            progressBar1.Value = Value;
            progressBar1.Refresh();
            Application.DoEvents();
        }
    }
}